//
//  LiftIMSystemManger.m
//  AAIM
//
//  Created by awh on 2019/12/29.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "LiftIMSystemManger.h"
#import "NSDictionary+JSONString.h"

#import "ReceiveResultModel.h"
#import "BTMessageModel.h"

@implementation LiftIMSystemManger

#pragma mark - public

- (void)addDelegate:(id<IMSystemManagerDelegate>)delegate {
    NSValue *delegateValue = [NSValue valueWithNonretainedObject:delegate];
    if (![self.delegates containsObject:delegateValue]) {
        [self.delegates addObject:delegateValue];
    }
}

- (void)removeDelegate:(id<IMSystemManagerDelegate>)delegate {
    NSValue *delegateValue = [NSValue valueWithNonretainedObject:delegate];
    if ([self.delegates containsObject:delegateValue]) {
        [self.delegates removeObject:delegateValue];
    }
}

- (void)receiveForcedToLogoutMessage {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.delegates enumerateObjectsUsingBlock:^(NSValue *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            id<IMSystemManagerDelegate> delegate = obj.nonretainedObjectValue;
            if ([delegate respondsToSelector:@selector(forcedToLogout)]) {
                [delegate forcedToLogout];
            }
        }];
    });
}
- (void)receiveSocketReconnectTimeout {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.delegates enumerateObjectsUsingBlock:^(NSValue *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            id<IMSystemManagerDelegate> delegate = obj.nonretainedObjectValue;
            if ([delegate respondsToSelector:@selector(socketReconnectTimeout)]) {
                [delegate socketReconnectTimeout];
            }
        }];
    });
}

/**
 socket连接状态发生改变
 
 @param status 0：连接失败  1：连接成功  2：正在连接
 */
- (void)receiveSocketConnectStatusDidChanged:(NSInteger)status {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.delegates enumerateObjectsUsingBlock:^(NSValue *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            id<IMSystemManagerDelegate> delegate = obj.nonretainedObjectValue;
            if ([delegate respondsToSelector:@selector(socketConnectStatusDidChanged:)]) {
                [delegate socketConnectStatusDidChanged:status];
            }
        }];
    });
}


#pragma mark - getter

- (NSMutableArray *)delegates {
    if (!_delegates) {
        _delegates = [NSMutableArray new];
    }
    return _delegates;
}


@end
